#include "aeampfeature.h"
#include <QHeaderView>
#include <QVBoxLayout>
#include <QString>
#include "ae/aeampspectrum.h"

AEAmpFeature::AEAmpFeature(QObject* parent)
    : QObject(parent)
    , m_dataTable(nullptr)
    , m_groupBox(nullptr)
    , m_ultrasonicSensorType(0)
    , m_dischargeTypeProbability("")
    , m_amplitudeUnit(0)
    , m_amplitudeLowerLimit(0)
    , m_amplitudeUpperLimit(0)
    , m_frequencyBand(0)
    , m_lowerFrequency(0)
    , m_upperFrequency(0)
    , m_testFrequency(0)
    , m_signalMaxValue(0)
    , m_signalRMSValue(0)
    , m_correlation50Hz(0)
    , m_correlation100Hz(0)
    , m_dataPointCount(0)
    , m_backgroundFileName("")
    , m_dataTypeCode(0)
    , m_dataLength(0)
    , m_createTime("")
    , m_chartProperty(DataSpecificationGW::PROPERTY_DEFAULT)
    , m_deviceName("")
    , m_deviceCode("")
    , m_measurementName("")
    , m_measurementCode("")
    , m_channelFlag(0)
    , m_dataType(0)
    , m_reservedData()
{
}

AEAmpFeature::~AEAmpFeature()
{
    // UI元素会随父对象自动删除
}

QWidget* AEAmpFeature::initUI(QWidget* parent)
{
    m_groupBox = new QGroupBox("AE幅值特性", parent);
    m_dataTable = new QTableWidget(26, 2, m_groupBox);
    
    // 设置表格属性
    m_dataTable->setHorizontalHeaderLabels(QStringList() << "名称" << "值");
    m_dataTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
    m_dataTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    m_dataTable->horizontalHeader()->setFixedHeight(45);
    m_dataTable->horizontalHeader()->setStyleSheet("QHeaderView::section{background:skyblue;}");
    
    // 设置表头
    QString headers[] = {
        "DataType(检测数据类型编码)",
        "DataLength(图谱数据长度)",
        "CreateTime(图谱生成时间)",
        "ChartProperty(图谱性质)",
        "DeviceName(被测设备名称)",
        "DeviceCode(被测设备编码)",
        "MeasurementName(测点名称)",
        "MeasurementCode(测点编码)",
        "ChannelFlag(检测通道标志)",
        "DataType(存储数据类型)",
        "AmplitudeUnit(幅值单位)",
        "AmplitudeLowerLimit(幅值下限)",
        "AmplitudeUpperLimit(幅值上限)",
        "UltrasonicSensorType(超声波传感器类型)",
        "DataPointCount(数据点数)",
        "TestFrequency(试验频率)",
        "DischargeTypeProbability(放电类型概率)",
        "SignalMaxValue(信号最大值)",
        "SignalRMSValue(信号有效值)",
        "Correlation50Hz(50Hz相关性)",
        "Correlation100Hz(100Hz相关性)",
        "FrequencyBand(频带)",
        "LowerFrequency(下限频率)",
        "UpperFrequency(上限频率)",
        "BackgroundFileName(背景文件名称)",
        "ReservedData(预留数据)"
    };
    
    for(int i = 0; i < 26; i++) {
        m_dataTable->setItem(i, 0, createTableWidgetItem(headers[i]));
    }
    
    // 创建布局
    QVBoxLayout* layout = new QVBoxLayout(m_groupBox);
    layout->addWidget(m_dataTable);
    
    return m_groupBox;
}

void AEAmpFeature::loadFromSpectrum(DataSpecificationGW::Spectrum* spectrum)
{
    if (!spectrum) return;
    
    DataSpecificationGW::AEAmpSpectrum* aeAmpSpectrum = 
        dynamic_cast<DataSpecificationGW::AEAmpSpectrum*>(spectrum);
    if (!aeAmpSpectrum) return;
    
    // 获取基本信息
    DataSpecificationGW::SpectrumHead stSpectrumHead;
    aeAmpSpectrum->getSpectrumHead(stSpectrumHead);
    
    // 设置基本信息数据
    setDataTypeCode(stSpectrumHead.eSpectrumTypeCode);
    setDataLength(stSpectrumHead.iSpectrumDataLength);
    setCreateTime(stSpectrumHead.qstrSpectrumGenerationTime);
    setChartProperty(stSpectrumHead.eSpectrumCharacter);
    setDeviceName(stSpectrumHead.qstrEquipmentName);
    setDeviceCode(stSpectrumHead.qstrEquipmentCode);
    setMeasurementName(stSpectrumHead.qstrTestPointName);
    setMeasurementCode(stSpectrumHead.qstrTestPointCode);
    setChannelFlag(stSpectrumHead.sDetectionChannelID);
    setDataType(stSpectrumHead.eStorageDataType);
    
    // 获取扩展信息
    DataSpecificationGW::AEAmpExtInformation extInfo;
    aeAmpSpectrum->getAEAmpExtInformation(extInfo);
    
    // 设置数据
    setUltrasonicSensorType(static_cast<float>(extInfo.eAESensorType));
    setDischargeTypeProbability(QString::number(extInfo.aucPDTypeProbability[0]));
    setAmplitudeUnit(static_cast<uint8_t>(extInfo.eAmpUnit));
    setAmplitudeLowerLimit(extInfo.fAmpLowerLimit);
    setAmplitudeUpperLimit(extInfo.fAmpUpperLimit);
    setFrequencyBand(static_cast<uint8_t>(extInfo.eFrequencyBand));
    setLowerFrequency(extInfo.fFrequencyLowerLimit);
    setUpperFrequency(extInfo.fFequencyUpperLimit);
    setTestFrequency(extInfo.fSystemFrequency);
    setDataPointCount(extInfo.iDataPoint);
    
    // 获取数据
    DataSpecificationGW::AEAmpData data;
    aeAmpSpectrum->getAEAmpData(data);
    
    // 设置数据
    setSignalMaxValue(data.fSignalMax);
    setSignalRMSValue(data.fSignalRMS);
    setCorrelation50Hz(data.fFrequencyComponent1);
    setCorrelation100Hz(data.fFrequencyComponent2);
    setBackgroundFileName(extInfo.qstrBGFileName);
    
    // 更新表格显示
    updateTable();
}

void AEAmpFeature::setUltrasonicSensorType(float type)
{
    m_ultrasonicSensorType = type;
    updateTable();
}

void AEAmpFeature::setDischargeTypeProbability(const QString& probability)
{
    m_dischargeTypeProbability = probability;
    updateTable();
}

void AEAmpFeature::setAmplitudeUnit(uint8_t unit)
{
    m_amplitudeUnit = unit;
    updateTable();
}

void AEAmpFeature::setAmplitudeLowerLimit(float lowerLimit)
{
    m_amplitudeLowerLimit = lowerLimit;
    updateTable();
}

void AEAmpFeature::setAmplitudeUpperLimit(float upperLimit)
{
    m_amplitudeUpperLimit = upperLimit;
    updateTable();
}

void AEAmpFeature::setFrequencyBand(uint8_t band)
{
    m_frequencyBand = band;
    updateTable();
}

void AEAmpFeature::setLowerFrequency(float lowerFreq)
{
    m_lowerFrequency = lowerFreq;
    updateTable();
}

void AEAmpFeature::setUpperFrequency(float upperFreq)
{
    m_upperFrequency = upperFreq;
    updateTable();
}

void AEAmpFeature::setTestFrequency(float frequency)
{
    m_testFrequency = frequency;
    updateTable();
}

void AEAmpFeature::setSignalMaxValue(float value)
{
    m_signalMaxValue = value;
    updateTable();
}

void AEAmpFeature::setSignalRMSValue(float value)
{
    m_signalRMSValue = value;
    updateTable();
}

void AEAmpFeature::setCorrelation50Hz(float correlation)
{
    m_correlation50Hz = correlation;
    updateTable();
}

void AEAmpFeature::setCorrelation100Hz(float correlation)
{
    m_correlation100Hz = correlation;
    updateTable();
}

void AEAmpFeature::setDataPointCount(int32_t count)
{
    m_dataPointCount = count;
    updateTable();
}

void AEAmpFeature::setBackgroundFileName(const QString& fileName)
{
    m_backgroundFileName = fileName;
    updateTable();
}

void AEAmpFeature::setDataTypeCode(uint8_t code)
{
    m_dataTypeCode = code;
    updateTable();
}

void AEAmpFeature::setDataLength(int32_t length)
{
    m_dataLength = length;
    updateTable();
}

void AEAmpFeature::setCreateTime(const QString& time)
{
    m_createTime = time;
    updateTable();
}

void AEAmpFeature::setChartProperty(const DataSpecificationGW::SpectrumCharacter& property)
{
    m_chartProperty = property;
    updateTable();
}

void AEAmpFeature::setDeviceName(const QString& name)
{
    m_deviceName = name;
    updateTable();
}

void AEAmpFeature::setDeviceCode(const QString& code)
{
    m_deviceCode = code;
    updateTable();
}

void AEAmpFeature::setMeasurementName(const QString& name)
{
    m_measurementName = name;
    updateTable();
}

void AEAmpFeature::setMeasurementCode(const QString& code)
{
    m_measurementCode = code;
    updateTable();
}

void AEAmpFeature::setChannelFlag(int64_t flag)
{
    m_channelFlag = flag;
    updateTable();
}

void AEAmpFeature::setDataType(uint8_t type)
{
    m_dataType = type;
    updateTable();
}

void AEAmpFeature::setReservedData(const QByteArray& data)
{
    m_reservedData = data;
    updateTable();
}

QTableWidgetItem* AEAmpFeature::createTableWidgetItem(const QString& text)
{
    QTableWidgetItem* item = new QTableWidgetItem(text);
    QFont font;
    font.setBold(true);
    item->setFont(font);
    item->setTextAlignment(Qt::AlignCenter);
    return item;
}

void AEAmpFeature::updateTable()
{
    if (!m_dataTable) return;
    
    // 更新所有数据项
    m_dataTable->setItem(0, 1, createTableWidgetItem(QString::number(m_dataTypeCode)));
    m_dataTable->setItem(1, 1, createTableWidgetItem(QString::number(m_dataLength)));
    m_dataTable->setItem(2, 1, createTableWidgetItem(m_createTime));
    m_dataTable->setItem(3, 1, createTableWidgetItem(QString::number(m_chartProperty)));
    m_dataTable->setItem(4, 1, createTableWidgetItem(m_deviceName));
    m_dataTable->setItem(5, 1, createTableWidgetItem(m_deviceCode));
    m_dataTable->setItem(6, 1, createTableWidgetItem(m_measurementName));
    m_dataTable->setItem(7, 1, createTableWidgetItem(m_measurementCode));
    m_dataTable->setItem(8, 1, createTableWidgetItem(QString::number(m_channelFlag)));
    m_dataTable->setItem(9, 1, createTableWidgetItem(QString::number(m_dataType)));
    m_dataTable->setItem(10, 1, createTableWidgetItem(QString::number(m_amplitudeUnit)));
    m_dataTable->setItem(11, 1, createTableWidgetItem(QString::number(m_amplitudeLowerLimit)));
    m_dataTable->setItem(12, 1, createTableWidgetItem(QString::number(m_amplitudeUpperLimit)));
    m_dataTable->setItem(13, 1, createTableWidgetItem(QString::number(m_ultrasonicSensorType)));
    m_dataTable->setItem(14, 1, createTableWidgetItem(QString::number(m_dataPointCount)));
    m_dataTable->setItem(15, 1, createTableWidgetItem(QString::number(m_testFrequency)));
    m_dataTable->setItem(16, 1, createTableWidgetItem(m_dischargeTypeProbability));
    m_dataTable->setItem(17, 1, createTableWidgetItem(QString::number(m_signalMaxValue)));
    m_dataTable->setItem(18, 1, createTableWidgetItem(QString::number(m_signalRMSValue)));
    m_dataTable->setItem(19, 1, createTableWidgetItem(QString::number(m_correlation50Hz)));
    m_dataTable->setItem(20, 1, createTableWidgetItem(QString::number(m_correlation100Hz)));
    m_dataTable->setItem(21, 1, createTableWidgetItem(QString::number(m_frequencyBand)));
    m_dataTable->setItem(22, 1, createTableWidgetItem(QString::number(m_lowerFrequency)));
    m_dataTable->setItem(23, 1, createTableWidgetItem(QString::number(m_upperFrequency)));
    m_dataTable->setItem(24, 1, createTableWidgetItem(m_backgroundFileName));
} 
